//
//  PersonalInfoViewController.m
//  HZHome
//
//  Created by SunShine on 13-10-16.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "PersonalInfoViewController.h"
#import "StreetInfoItem.h"
#import "DataStoreService.h"
#import "StreetInfoService.h"
//保存数据
//[DataStoreService saveToDefaults:CURRENT_SELECTED_STREET_ID_KEY data:item.item_id];
//[DataStoreService saveToDefaults:CURRENT_SELECTED_STREET_NAME_KEY data:item.item_name];
//[StreetInfoService getService].selectedStreetId = item.item_id;
@interface PersonalInfoViewController ()
@property (strong, nonatomic) CommitteesByStreetService *streetService;
@property (strong, nonatomic) NSArray *streetArray;
@property (assign, nonatomic) NSInteger currentStreetId;
-(NSString *)getStreetNameById:(NSInteger)streetId;
@end

@implementation PersonalInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"我的信息";
    //开始读取街道列表
    self.streetService = [[CommitteesByStreetService alloc]init];
    self.streetService.delegate = self;
    [self.streetService loadDataWithKey:nil];
    [self startLoadingView];
    //街道 信息
    
    UserInfo *userInfo = [UserInfoService getUserInfoService].userInfo;
    if (userInfo.streetId && userInfo.streetId != (id)[NSNull null]) {
        self.currentStreetId = userInfo.streetId.integerValue;
    }
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //初始化读入数据
    UserInfo *userInfo = [UserInfoService getUserInfoService].userInfo;
    self.userNameField.placeholder = userInfo.userName;
    self.userPhoneField.text = userInfo.mobile;
    self.userPhoneField.keyboardType = UIKeyboardTypeNumberPad;
    self.userPhoneField.inputAccessoryView = [self getInputAccessoryView];
    self.userAddressField.text = userInfo.address;
    self.userAddressField.inputAccessoryView = [self getInputAccessoryView];
    [self.streetButton setTitle:[self getStreetNameById:self.currentStreetId] forState:UIControlStateNormal];
    //常住人口
    NSString *permanent = userInfo.permanent;
    if (![permanent isKindOfClass:[NSNull class]]) {
        if (nil != permanent && permanent.integerValue == 1) {
            self.residentSwitch.on = YES;
        } else{
            self.residentSwitch.on = NO;
        }
    }

//    if (permanent && permanent != (id)[NSNull null] && (NSInteger)permanent == 1) {
//        self.residentSwitch.on = YES;
//    } else {
//        self.residentSwitch.on = NO;
//    }
    //在此工作
     NSString *workHere = userInfo.workHere;
    if (nil != workHere && workHere.integerValue == 1) {
        self.onWorkSwitch.on = YES;
    }else{
        self.onWorkSwitch.on = NO;
    }
   
//    if (workHere && workHere != (id)[NSNull null] && (NSInteger)workHere == 1) {
//        self.onWorkSwitch.on = YES;
//    } else {
//        self.onWorkSwitch.on = NO;
//    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)streetButtonAction:(id)sender
{
    ChooseStreetViewController *chooseVC = [[ChooseStreetViewController alloc]initWithNibName:@"ChooseStreetViewController" bundle:nil];
    chooseVC.delegate = self;
    chooseVC.streetListService = self.streetService;
    chooseVC.usedForPersonalInfo = YES;
    chooseVC.selectedStreetId = self.currentStreetId;
    [self.navigationController pushViewController:chooseVC animated:YES];
}
//提交按扭
- (IBAction)commitButtonAction:(id)sender
{
    [UserInfoService getUserInfoService].delegate = self;
    [self startLoadingViewWithText:@"正在保存..."];
    [[UserInfoService getUserInfoService] modifyUserInfoWithMobile:self.userPhoneField.text Address:self.userAddressField.text StreetId:self.currentStreetId Permanent:self.residentSwitch.on WorkHere:self.onWorkSwitch.on];
}

-(NSString *)getStreetNameById:(NSInteger)streetId
{
    NSString *name = @"";
    for (StreetInfoItem *item in self.streetArray) {
        if (item.item_id.integerValue == streetId) {
            name = item.item_name;
            break;
        }
    }
    return name;
}
//列表回调
#pragma mark - Data Service Delegate
-(void)loadDataFinished
{
    [self stopLoadingView];
    self.streetButton.enabled = YES;
    self.streetArray = self.streetService.dataList;
    UserInfo *userInfo = [UserInfoService getUserInfoService].userInfo;
    if (userInfo&&!isBlankString(userInfo.streetId)) {
        [self.streetButton setTitle:[self getStreetNameById:userInfo.streetId.integerValue] forState:UIControlStateNormal];
        
        //保存到本地
//        [DataStoreService saveToDefaults:CURRENT_SELECTED_STREET_ID_KEY data:userInfo.streetId];
//        [DataStoreService saveToDefaults:CURRENT_SELECTED_STREET_NAME_KEY data:@"sdf"];
////        self getStreetNameById:userInfo.streetId
//        [StreetInfoService getService].selectedStreetId = userInfo.streetId;
    }
}
-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"读取街道列表失败,无法修改街道信息,请稍后重试." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    self.streetButton.enabled = NO;
}

-(void)setEnableToLoadMore:(BOOL)enable
{
    //did nothing
}
//修改成功回调
#pragma mark - User Service Delegate
-(void)loadUserInfoFinished:(id)result
{
    [self stopLoadingView];
    //保存选中的街道 id 到全局变量
    UserInfo *globalUserInfo = [UserInfoService getUserInfoService].userInfo;
 
    //保存到全局变量
    globalUserInfo.streetId =   [NSString stringWithFormat: @"%d", self.currentStreetId];
 
    globalUserInfo.permanent = self.residentSwitch.on?@"1":@"0";
    globalUserInfo.workHere = self.onWorkSwitch.on?@"1":@"0";
    globalUserInfo.address = self.userAddressField.text;
 
    //保存用户信息
//    [[UserInfoService getUserInfoService] loginSuccessHandler:result];
}

-(void)loadUserInfoFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - Choose Callback
-(void)choosedStreet:(NSInteger)streetId
{
    NSLog(@"选择了街道ID:%d",streetId);
    self.currentStreetId = streetId;
    [self.streetButton setTitle:[self getStreetNameById:streetId] forState:UIControlStateNormal];
    
    
    
    
}
@end
